﻿using Avalonia.Controls;
using LightCAD.Core;
using LightCAD.Runtime;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LightCAD.UI
{
    public partial class ColorDropDown : DropDownControl
    {
        private List<string> colorList = new List<string>() { "ByLayer", "ByBlock", "红", "黄", "绿", "青", "蓝", "洋红" };
        private ColorItemControl colorItemControl;
        private List<LcElement> lcElementsList = new List<LcElement>();
        private LcDocument lcDocument;
        private uint selectColor;
        public ColorDropDown()
        {
            InitializeComponent();
            InitializeDropDown(itemsPanel);
            
        }
        public ColorDropDown(LcDocument lcDocument):this()
        {
            this.selectColor = lcDocument.Layers.First().Color;
            colorItemControl = new ColorItemControl();
            colorItemControl.Location = new Point(0, 0);
            colorItemControl.Name = "colorItemControl1";
            colorItemControl.Size = new Size(155, 24);
            colorItemControl.TabIndex = 0;
            colorItemControl.ItemClick += ColorItemControl_ItemClick;
            this.Controls.Add(colorItemControl);
            InitColors(lcDocument);

        }

        private void ColorItemControl_ItemClick(object? sender, string e)
        {
            OpenDropDown();
        }

        public void InitColors(LcDocument lcDocument)
        {
            this.lcDocument = lcDocument;
            itemsPanel.Controls.Clear();
            foreach (var color in colorList)
            {
                var colorItem = new ColorItemControl(color);
                colorItem.ItemClick += ColorItem_ItemClick;
                itemsPanel.Controls.Add(colorItem);
            }
            this.Refresh();
        }
        private void ColorItem_ItemClick(object? sender, string e)
        {
            if (sender != null && sender is uint)
            {
                var cl = (uint)sender;
                switch (e)
                {
                    case "Name":
                        this.selectColor = cl;
                        foreach (LcLayer lcLayer in this.lcDocument.Layers)
                        {
                            lcLayer.IsStatus = this.selectColor == lcLayer.Color;
                        }
                        this.colorItemControl.Reset(selectColor, lcElementsList);
                        break;
                    default:
                        if (cl == this.selectColor)
                        {
                            this.colorItemControl.Reset(selectColor, null);
                        }
                        break;
                }
            }

        }
        public void ResetSelectColor(SelectedEventArgs args)
        {
            this.colorItemControl.Visible = true;
            if (!args.Selected)
            {
                lcElementsList.Clear();
                this.selectColor = lcDocument.Layers.First(n => n.IsStatus).Color;
                this.colorItemControl.Reset(selectColor, null);
            }
            else
            {
                var lcElements = args.Elements;
                lcElementsList.AddRange(lcElements);
                if (lcElementsList.GroupBy(n => n.Layer).Count() > 1)
                {
                    this.selectColor = 0;
                    this.colorItemControl.Visible = false;
                }
                else
                {
                    this.selectColor = lcDocument.Layers.First(n => n.Name == lcElements.First().Layer).Color;
                    this.colorItemControl.Reset(selectColor, null);

                }
            }
        }
    }
}
